package com.hqj.config;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class FilterConfig extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext requestContext = RequestContext.getCurrentContext();
        try{
            doBizProcess(requestContext);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    public void doBizProcess(RequestContext requestContext) throws Exception{
        HttpServletRequest request = requestContext.getRequest();
        System.out.println("过滤器启动");
        //获取出请求的路径
        String uri = request.getRequestURI();
        if(!uri.contains("getProvider")){
            requestContext.setSendZuulResponse(false);
            requestContext.setResponseStatusCode(401);
            requestContext.getResponse().setCharacterEncoding("UTF-8");
            requestContext.getResponse().setContentType("text/html;character=utf-8");
            requestContext.getResponse().getWriter().print("路径出现错误.....");

        }
    }
}
